﻿Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports DotNetNuke
Namespace NVPortal.Modules.NVNews

    Public MustInherit Class View
        Inherits Entities.Modules.PortalModuleBase

        Public Function ReplaceChuoi(ByVal chuoi As String) As String
            Dim convert As String = Global.ReplaceChuoi.bodau(chuoi).Replace(" ", "-")
            Return convert
        End Function
#Region "Event Handlers"
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try
                If Not ViewState("startdate") Is Nothing Then
                    Me.txtStartdate.Text = ViewState("startdate").ToString
                    Me.txtEndDate.Text = ViewState("enddate").ToString
                End If
                If Not IsPostBack Then
                    binddata()
                End If
            Catch ex As Exception
                ProcessModuleLoadException(Me, ex)
            End Try
        End Sub
        Private Sub binddata()
            BindddlCategories()
            BinddrgDataViewer()
        End Sub

        Private Sub BinddrgDataViewer()
            Try
                ' default find all new that need to approve
                Dim ctlNews As New NV_NewsController
                Dim strStartDate As String = "1/1/1900"
                Dim strEndDate As String = "1/1/2100"
                If Me.txtStartdate.Text <> "" Then
                    strStartDate = Me.txtStartdate.Text
                End If
                If Me.txtEndDate.Text <> "" Then
                    strEndDate = Me.txtEndDate.Text
                End If

                Dim ds As ArrayList
                ds = ctlNews.FindByStatus(strStartDate, strEndDate, Trim(Me.txtTitle.Text), Me.ddlCategory.SelectedValue, PortalId, 2, UserId)

                Me.drgDataViewer.DataSource = ds
                Me.drgDataViewer.PageSize = Me.ddlPageSize.SelectedValue
                Me.drgDataViewer.DataBind()

                Me.lbTotalNewsCount.Text = ds.Count
            Catch ex As Exception
                Response.Write(ex.ToString)
            End Try
        End Sub

        Private Sub BindddlCategories()
            Dim ctlNewsCategories As New NV_NewsCategoriesController
            Dim arrNewsCategories As New ArrayList
            arrNewsCategories = ctlNewsCategories.GetAll(PortalId)
            Dim arrTemp As New ArrayList
            Dim objNewsCategories As NV_NewsCategoriesInfo
            Dim objNewsCategoriesTemp As NV_NewsCategoriesInfo

            If arrNewsCategories.Count > 0 Then
                For Each objNewsCategories In arrNewsCategories
                    If objNewsCategories.ParentId = 0 Then
                        arrTemp.Add(objNewsCategories)
                        For Each objNewsCategoriesTemp In arrNewsCategories
                            If objNewsCategoriesTemp.ParentId = objNewsCategories.CategoryID Then
                                objNewsCategoriesTemp.CategoryName = "--" & objNewsCategoriesTemp.CategoryName
                                arrTemp.Add(objNewsCategoriesTemp)
                            End If
                        Next
                    End If
                Next
            End If

            Me.ddlCategory.DataSource = arrTemp
            Me.ddlCategory.DataTextField = "CategoryName"
            Me.ddlCategory.DataValueField = "CategoryId"
            Me.ddlCategory.DataBind()
            Me.ddlCategory.Items.Insert(0, New ListItem("--Tất cả thư mục--", 0))

        End Sub
#End Region

        Private Sub lbtFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbtFind.Click
            Try
                ' default find all new that need to approve
                Me.drgDataViewer.CurrentPageIndex = 0
                BinddrgDataViewer()
            Catch ex As Exception
                ProcessModuleLoadException(Me, ex)
            End Try
        End Sub
        Protected Sub drgDataViewer_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles drgDataViewer.PageIndexChanged
            Try

                Me.drgDataViewer.CurrentPageIndex = e.NewPageIndex

            Catch ex As Exception
                Me.drgDataViewer.CurrentPageIndex = 0

            End Try
            BinddrgDataViewer()
        End Sub

        Public Function GetUserName(ByVal userid As Integer) As String
            If userid = -1 Then
                GetUserName = "Độc giả"
            Else
                Return UserController.GetUser(PortalId, userid, True).Username
            End If
        End Function

        Protected Sub ddlPageSize_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlPageSize.SelectedIndexChanged
            Me.drgDataViewer.CurrentPageIndex = 0
            BinddrgDataViewer()
        End Sub
    End Class

End Namespace
